//
//  PhotoKitViewController.m
//  XiangJi
//
//  Created by apple on 2017/8/9.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "PhotoKitViewController.h"
#import <Photos/Photos.h>
@interface PhotoKitViewController ()

@property (nonatomic, strong) UIImageView *imagV;

@end

@implementation PhotoKitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.imagV = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    
    [self.view addSubview:_imagV];
    
    // 获取所有资源的集合，并按资源的创建时间排序
    PHFetchOptions *options = [[PHFetchOptions alloc] init];
    options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
    PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];

    __weak typeof(self) weakSelf = self;
    // 这时 assetsFetchResults 中包含的，应该就是各个资源（PHAsset）
    for (NSInteger i = 0; i < assetsFetchResults.count; i++) {
        // 获取一个资源（PHAsset）
        PHAsset *asset = assetsFetchResults[i];
        
        PHImageManager *imag = [[PHImageManager alloc] init];
        
        [imag requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            
            weakSelf.imagV.image = result;
            
            NSString *ret = info[@"PHImageFileSandboxExtensionTokenKey"];
            
            NSString *reser = [ret substringWithRange:NSMakeRange(0, 40)];
            
            NSLog(@"result = %@",result);
            NSLog(@"info = %@",reser);
        }];
        
    
        
//        CGImageRef fullResolutionImageRef = [options fullResolutionImage];
//        // 通过 fullResolutionImage 获取到的的高清图实际上并不带上在照片应用中使用“编辑”处理的效果，需要额外在 AlAssetRepresentation 中获取这些信息
//        NSString *adjustment = [[_alAssetRepresentation metadata] objectForKey:@"AdjustmentXMP"];
//        if (adjustment) {
//            // 如果有在照片应用中使用“编辑”效果，则需要获取这些编辑后的滤镜，手工叠加到原图中
//            NSData *xmpData = [adjustment dataUsingEncoding:NSUTF8StringEncoding];
//            CIImage *tempImage = [CIImage imageWithCGImage:fullResolutionImageRef];
//            
//            NSError *error;
//            NSArray *filterArray = [CIFilter filterArrayFromSerializedXMP:xmpData
//                                                         inputImageExtent:tempImage.extent
//                                                                    error:&error];
//            CIContext *context = [CIContext contextWithOptions:nil];
//            if (filterArray && !error) {
//                for (CIFilter *filter in filterArray) {
//                    [filter setValue:tempImage forKey:kCIInputImageKey];
//                    tempImage = [filter outputImage];
//                }
//                fullResolutionImageRef = [context createCGImage:tempImage fromRect:[tempImage extent]];
//            }
//        }
//        // 生成最终返回的 UIImage，同时把图片的 orientation 也补充上去
//        resultImage = [UIImage imageWithCGImage:fullResolutionImageRef scale:[_alAssetRepresentation scale] orientation:(UIImageOrientation)[_alAssetRepresentation orientation]];
    }
    
    


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
